//
//  VisitorView.swift
//  weibosina
//
//  Created by zhangzhen on 16/6/4.
//  Copyright © 2016年 zhangzhen. All rights reserved.
//

import UIKit

//定义一个协议
protocol VisitorButtonDelegate: NSObjectProtocol {
    //点击注册按钮
    func visitorRegisterButtonDid()
    
    
    func visitorLoginButtonDid()
}


class VisitorView: UIView {
    
    //设置代理,来监听按钮的点击事件
    weak var delegate:VisitorButtonDelegate?
    
   //实现点击方法
    @objc func visitorRegisterButtonDid() {
        
        delegate?.visitorRegisterButtonDid()
    }
    @objc func visitorLoginButtonDid() {
        
        delegate?.visitorLoginButtonDid()
        
    }
    
   
    
    
    
    
    //写一个方法来实现,加载不同的文字和图片
    //根据传过来的imageName和title来设置文字
    func setInfo(imageName: String?, title: String?) {
        
        guard let imgName=imageName else {
            //直接返回
            return
        }
        titleLabel.text = title
        iconView.image = UIImage(named: imgName)
        //隐藏动画
        ringView.hidden = true
        
        
    }
    
    //加载动画
    private func starAnim(){
        //1.创建一个动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //设置动画不会被销毁
        anim.removedOnCompletion = false
        
        ringView.layer.addAnimation(anim, forKey: nil)
        
        
        
    }


 
    
    //MARK - 懒加载子控件
    
    //2.环加载
    private lazy var ringView : UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //1.小房子加载
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //---添加遮罩物
    private lazy var shadeView : UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //3.textLabel加载
    private lazy var titleLabel : UILabel = UILabel(textTitle: "关注一些人，回这里看看有什么惊喜关注一些人关注一些人，回这里看看有什么惊喜关注一些人", textTitleColor: UIColor.grayColor())
    
    //4.添加注册button加载
    private lazy var registerButton : UIButton = UIButton(imageName: "common_button_white_disable", titleName: "注册", color: UIColor.orangeColor())
        
    
    //5.添加登录button加载
    private lazy var loginButton : UIButton = UIButton(imageName: "common_button_white_disable", titleName: "登录", color: UIColor.grayColor())
    
    
    
    
    
    //在initWithFrame,中添加子控件
    override init(frame: CGRect) {
        super.init(frame:frame)
        //添加子控件
        setUI()
        starAnim()
        
    }
    //必须实现,防止从sb和xib中加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}


//布局子控件
extension VisitorView{
    
    //添加UI控件
    private func setUI(){
        //1.添加view
        addSubview(ringView)
        addSubview(shadeView)
        addSubview(iconView)
        
        addSubview(titleLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //2.设置自动布局
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //3.布局子控件
        
        //3.2设置ringView布局
        addConstraint(NSLayoutConstraint(item: ringView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: ringView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //3.1设置iconView布局
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: ringView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: ringView, attribute: .CenterY, multiplier: 1.0, constant: 0))
       
        //3.3布局titleLabel
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: ringView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //设置label 的长度
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        
        //3.4布局注册button按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //3.5布局登录
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //3.6 布局遮罩物图片
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[shade]-0-|", options: [], metrics: nil, views: ["shade":shadeView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[shade]-(btnheight)-[register]", options: [], metrics: ["btnheight":-36], views: ["shade":shadeView,"register":registerButton]))
//        print(registerButton.bounds.height)
        
        //给当前的view设置一个背景颜色
        backgroundColor = UIColor(colorLiteralRed: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
       
        //4.设置登录和注册按钮的点击事件
        registerButton.addTarget(delegate, action: "visitorRegisterButtonDid", forControlEvents: .TouchUpInside)
        loginButton.addTarget(delegate, action: "visitorLoginButtonDid", forControlEvents: .TouchUpInside)
    }
    
    
    
    
}





